$(document).ready(function() {
	$('#parentSearch').click(function() {
		$.ajax({
			type: "POST",
			url: "adapter/parentSearch.php",
			data: $('input#parentSearchInput').serialize(),
			beforeSend: function (){
				$("#loading").show();
			},
			}).done(function(msg) {
				$("#searchResult").html("");
				$("#loading").hide();
				if (msg.CODE_RETOUR == 0) {
					//$("#error").show();
				}
				else {
					var obj = jQuery.parseJSON (msg);
					for (var i = 0; i < obj.length; i++) {
						var html = "<div class='item' id='item' idParent='" + obj[i].ID_PARENT + "'>" + obj[i].PRENOM + " " + obj[i].NOM + "</div><div class='showEleve' id='" + obj[i].ID_PARENT + "'></div>";
						$("#searchResult").append(html);
					}
				}
			$('div#item').click(function() {
				$('div.showEleve').slideUp("slow");
				$('div.showEleve').html("");
				var idParent = $(this).attr("idParent");
				$("#" + idParent).html("");
				eleveSearch(idParent);
				$('div.showEleve').slideDown("slow");
			});
		});
	});
	
	function eleveSearch (idParent) {
		$.ajax({
			type: "POST",
			url: "adapter/eleveSearch.php",
			data: {"idParent":idParent},
			beforeSend: function (){
				$("#loading").show();
			},
			}).done(function(msg) {
				$("#loading").hide();
				$("#" + idParent).append(msg);
		});
	}
});